﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;


namespace NotebookS2
{
    public partial class M_WHOUSE : RibbonForm
    {
        public M_WHOUSE()
        {
            InitializeComponent();
        }

        private DataTable DataWarehouses { get; set; }
        public string ConnectionString { get; set; }

        #region Methods

        DataTable GetTable()
        {
            DataTable table = new DataTable();

            table.Columns.Add("Code", typeof(string));
            table.Columns.Add("Dscription", typeof(string));
            table.Columns.Add("AcctCode", typeof(string));
            table.Columns.Add("AcctName", typeof(string));
            table.Columns.Add("DocStatus", typeof(string));
            

            return table;
        }

        DataTable BindWarehouse()
        {
            WarehouseManager oManager = new WarehouseManager();
            
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable tableWH = oManager.GetWarehouse();
            oManager.Close();

            return tableWH;
        }

        #endregion
        private void M_WHOUSE_Load(object sender, EventArgs e)
        {
            this.DataWarehouses = BindWarehouse();

            gridControl1.DataSource = this.DataWarehouses;
            gridControl1.Refresh();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DataRow oRow = this.DataWarehouses.NewRow();

            WarehouseManager oManager = new WarehouseManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            if (txtCode.Text.Trim() != "" || txtDscription.Text.Trim() != "")
            {
                if (oManager.IsWarehouseCodeExists(txtCode.Text) == false)
                {
                    if (oManager.IsWarehouseNameExists(txtDscription.Text) == false)
                    {
                        oRow["Code"] = txtCode.Text;
                        oRow["Dscription"] = txtDscription.Text;
                        oRow["DocStatus"] = "Active";

                        this.DataWarehouses.Rows.Add(oRow);

                        gridControl1.DataSource = this.DataWarehouses;
                        gridControl1.Refresh();

                        txtCode.Text = "";
                        txtDscription.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Warehouse Description is already exists!!!", "Warehouse", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtDscription.Focus();
                        txtDscription.SelectAll();
                    }
                }
                else
                {
                    MessageBox.Show("Warehouse code is already exists!!!", "Warehouse", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }
            }
            else
            {
                MessageBox.Show("Code and Description should have information!!!", "Warehouse", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCode.Focus();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                WarehouseManager oManager = new WarehouseManager();
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                foreach (DataRow oRow in this.DataWarehouses.Rows)
                {
                    WarehouseUnit oUnit = new WarehouseUnit();
                    if (oRow.RowState == DataRowState.Added)
                    {
                        oUnit.Code = oRow["Code"].ToString();
                        oUnit.Description = oRow["Dscription"].ToString();
                        oUnit.AcctCode = oRow["AcctCode"].ToString();
                        oUnit.AcctName = oRow["AcctName"].ToString();
                        oUnit.Status = oRow["DocStatus"].ToString();

                        oManager.AddWarehouse(oUnit);
                    }
                    else if (oRow.RowState == DataRowState.Modified)
                    {
                        oUnit.Code = oRow["Code"].ToString();
                        oUnit.Description = oRow["Dscription"].ToString();
                        oUnit.AcctCode = oRow["AcctCode"].ToString();
                        oUnit.AcctName = oRow["AcctName"].ToString();
                        oUnit.Status = oRow["DocStatus"].ToString();

                        oManager.UpdateWarehouse(oUnit);
                    }
                }
                oManager.Close();

                this.DataWarehouses.AcceptChanges();
                MessageBox.Show("Warehouse is successfully updated!!!", "Warehouse", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error thrown by the sytem. " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtAcctCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;

            oForm.ShowDialog();

            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctCode"], oForm.SelectedGLAcctCode);
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctName"], oForm.SelectedGLAcctName);


        }

        private void txtAcctCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DevExpress.XtraEditors.TextEdit _TEXT_ACCOUNTCODE = (DevExpress.XtraEditors.TextEdit)sender;

                ChartOfAccountManager oManager = new ChartOfAccountManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable dt = oManager.GetChartOfAccountByAcctCode(_TEXT_ACCOUNTCODE.Text);
                oManager.Close();


                if (dt.Rows.Count > 0)
                {
                    DataRow oRow = dt.Rows[0];

                    string sAcctCode = oRow["AcctCode"].ToString();
                    string sAcctName = oRow["AcctName"].ToString();
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctCode"], sAcctCode);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctName"], sAcctName);


                }

                else
                {

                    FND_GLACCOUNT oForm = new FND_GLACCOUNT();

                    DevExpress.XtraEditors.TextEdit eGLAccount = (DevExpress.XtraEditors.TextEdit)sender;

                    oForm.ConnectionString = this.ConnectionString;
                    oForm.SetSearch(eGLAccount.Text);
                    oForm.ShowDialog();

                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctCode"], oForm.SelectedGLAcctCode);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["AcctName"], oForm.SelectedGLAcctName);

                }
            }
        }

    }
}
